package com.kurancy.web.controller;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Enumeration;

import javax.net.ssl.HttpsURLConnection;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.kurancy.service.KurancyNoteService;

@Controller
public class PaypalPaymentReturnNotificationController {
	
	@Autowired
	private KurancyNoteService kurancyNoteService;
	
	/** The logger. */
	private static Log logger = LogFactory.getLog(PaypalPaymentReturnNotificationController.class);
	
	@RequestMapping(value="/payment/ipn")
	public void ipn(HttpServletRequest request, @RequestParam (value="payKey") String payKey)  {
		
		try {
			// read post from PayPal system and add 'cmd'
			Enumeration en = request.getParameterNames();
			StringBuffer strBuffer = new StringBuffer("cmd=_notify-validate");
			String paramName;
			String paramValue;
			while (en.hasMoreElements()) {
				paramName = (String) en.nextElement();
				paramValue = request.getParameter(paramName);
				strBuffer.append("&").append(paramName).append("=")
						.append(URLEncoder.encode(paramValue));
			}
	
			// post back to PayPal system to validate
			// NOTE: change http: to https: in the following URL to verify using SSL (for increased security).
			// using HTTPS requires either Java 1.4 or greater, or Java Secure Socket Extension (JSSE)
			// and configured for older versions.
			URL u = new URL("https://www.paypal.com/cgi-bin/webscr");
			HttpsURLConnection uc = (HttpsURLConnection) u.openConnection();
			uc.setDoOutput(true);
			uc.setRequestProperty("Content-Type",
					"application/x-www-form-urlencoded");
			uc.setRequestProperty("Host", "www.paypal.com");
			PrintWriter pw = new PrintWriter(uc.getOutputStream());
			pw.println(strBuffer.toString());
			pw.close();
	
			BufferedReader in = new BufferedReader(new InputStreamReader(
					uc.getInputStream()));
			String res = in.readLine();
			in.close();
	
			// assign posted variables to local variables
			String itemName = request.getParameter("item_name");
			String itemNumber = request.getParameter("item_number");
			String paymentStatus = request.getParameter("payment_status");
			String paymentAmount = request.getParameter("mc_gross");
			String paymentCurrency = request.getParameter("mc_currency");
			String txnId = request.getParameter("txn_id");
			String receiverEmail = request.getParameter("receiver_email");
			String payerEmail = request.getParameter("payer_email");
	
			// check notification validation
			if (res.equals("VERIFIED")) {
				// check that paymentStatus=Completed
				// check that txnId has not been previously processed
				// check that receiverEmail is your Primary PayPal email
				// check that paymentAmount/paymentCurrency are correct
				// process payment
			} else if (res.equals("INVALID")) {
				// log for investigation
			} else {
				// error
			}
			//kurancyNoteService.markNotePaid(payKey);
		} catch (Exception e) {
			logger.error(e,e);
		}
	}
	
//	@RequestMapping(value="/payment/return", method = RequestMethod.GET)
//	public void returnPayment(@RequestParam (value="payKey") String payKey) {
//		
//		logger.debug("Started:");
//		
//		kurancyNoteService.markNotePaid(payKey);
//	}
	
//	@RequestMapping(value="/payment/cancel", method = RequestMethod.GET)
//	public void cancelledPayment(@RequestParam (value="payKey") String payKey) {
//		
//		logger.debug("Started:");
//		
//		kurancyNoteService.markNoteCancelled(payKey);
//	}
}
